import React, { useEffect } from "react";
import { Button, Form, Input, InputNumber, Radio, type FormProps } from "antd";
import type { listTS } from '../../store/listTS'
import { useAppDispatch, useAppSelector } from '../../store/getstateTs'
import * as asyncfn from '../../store/slice'
interface Iprops {
    onCancel: () => void,
    itemobj: listTS | {},
    title: string
}
const Activeform: React.FC<Iprops> = (props) => {
    let dispatch = useAppDispatch()
    let obj = useAppSelector(state => state.crms1)
    const [form] = Form.useForm();
    useEffect(() => {
        form.setFieldsValue(props.itemobj)
    }, [])
    const onFinish: FormProps<listTS>["onFinish"] = (values) => {
        if (props.title == '添加参数') {
            console.log(111)
            let obj = {
                ...values,
                'id': new Date().getTime()
            }
            console.log(obj);
            
            dispatch(asyncfn.async_addlist(obj))
        } else {
            console.log(222)
            let obj = {
                ...values,
                "id": props.itemobj.id
            }
            dispatch(asyncfn.async_editlist(obj))
        }
        props.onCancel()
    };
    return (
        <Form onFinish={onFinish} form={form}>
            <Form.Item
                label="优惠卷名称"
                name="title"
                rules={[{ required: true, message: "请输入名称!" }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="优惠券面值"
                name="facevalue"
                rules={[{ required: true, message: "请输入键名!" }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="优惠券最低消费"
                name="consume"
                rules={[{ required: true, message: "请输入键值!" }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="优惠券有效期限"
                name="deadline"
                rules={[{ required: true, message: "请输入价格!" }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="排序"
                name="sort"
                rules={[{ required: true, message: "请输入排序!" }]}
            >
                <InputNumber />
            </Form.Item>
            <Form.Item
                label="状态"
                name="state">
                <Radio.Group
                    options={[
                        { value: true, label: "开启" },
                        { value: false, label: "关闭" },
                    ]}
                />
            </Form.Item>
            <Form.Item>
                <Button htmlType="reset">重置</Button>
                <Button type='primary' htmlType="submit" >完成</Button>
            </Form.Item>
        </Form>
    );
};

export default Activeform;
